<?php
session_start();

require_once('./includes/smarty/Smarty.class.php');
require_once('./includes/functions.php');
require_once('./includes/api/class.api.php');
require_once('./includes/api/class.charselect.php');
require_once('./includes/api/class.charactername.php');

// Create and configure smarty instance
$smarty = new Smarty();
$smarty->template_dir   = './templates';
$smarty->compile_dir    = './includes/smarty/templates_c';
$smarty->cache_dir      = './includes/smarty/cache';
$smarty->config_dir     = './includes/smarty/configs';
$smarty->left_delimiter = '[{';
$smarty->right_delimiter= '}]';
$smarty->caching        = 1;
$smarty->cache_lifetime = 300;
$smarty->assign('pagetitle','Invention Tracker');

if(!isset($_GET['a']) && !isset($_SESSION['access'])) {
  $pagetoload = 'login';
} else {
  $pagetoload = $_GET['a'];
}

switch ($pagetoload) {
  // -- ADMIN PAGE --
  case 'admin':
  $users = Functions::getUsers();
  $smarty->clear_cache('users.tpl');
  $smarty->assign('users', $users);
  $smarty->assign('username', '');
  $smarty->display('header.tpl');
  $smarty->display('users.tpl');
  break;

  // -- EDIT USER --
  case 'edit-user':
  $users = Functions::getUsers();
  $smarty->assign('username', $_GET['name']);
  $smarty->assign('userid', $_GET['id']);
  $smarty->assign('users', $users);
  $smarty->clear_cache('users.tpl');
  $smarty->display('header.tpl');
  $smarty->display('users.tpl');
  break;

  // -- PROCESS EDITED USER --
  case 'edited':
  Functions::setUserAccess($_POST);
  $users = Functions::getUsers();
  $smarty->clear_cache('users.tpl');
  $smarty->assign('users', $users);
  $smarty->assign('username', '');
  $smarty->display('header.tpl');
  $smarty->display('users.tpl');
  break;

  // -- LOGIN PAGE --
  case 'login':
  $smarty->display('header.tpl');  
  $smarty->display('login.tpl');
  break;

  // -- LOGOUT --
  case 'logout':
  session_unset();
  $smarty->clear_cache('header.tpl');
  $smarty->display('header.tpl');  
  $smarty->display('login.tpl');
  break;

  // -- REGISTRATION PAGE --
  case 'register':
  $smarty->display('header.tpl');  
  $smarty->display('register.tpl');
  break;

  // -- PROCESS REGISTRATION
  case 'registered':
  $smarty->clear_cache('register.tpl');
  if(!isset($_POST['charid'])) {
    $api = new Api();
    $api->setCredentials($_POST['userid'],$_POST['apikey']);
    $apicharsxml = $api->getCharacters();
    $apichars    = Characters::getCharacters($apicharsxml);
    $smarty->assign('post', $_POST);
    $smarty->assign('chars', $apichars);
    $smarty->display('header.tpl');
    $smarty->display('register.tpl');
  } else {
    if($_POST['passwd1'] == $_POST['passwd2']){
      $passwd = md5($_POST['passwd1']);
      Functions::registerNew($_POST['username'], $passwd, $_POST['apikey'], $_POST['userid'], $_POST['charid']);
    }
    unset($_POST['charid']);
    unset($api);
    unset($apichars);
  }
  $smarty->display('header.tpl');
  $smarty->display('login.tpl');
  break;

  // -- PROCESS LOGIN --
  case 'logged':
  $user = strtoupper($_POST['username']);
  $passwd = md5($_POST['passwd']);
  $access = Functions::checkLogin($user, $passwd);
  if($access > 0) {
    $_SESSION['access'] = $access;
    if(!$smarty->is_cached('jobs.tpl')) {
      $jobs = Functions::getIndustryJobs();
      $smarty->assign('jobs',$jobs);
    }
    $smarty->clear_cache('header.tpl');
    $smarty->display('header.tpl');
    $smarty->display('jobs.tpl');
  } else {
    
    session_unset();
    $smarty->display('header.tpl');
    $smarty->display('login.tpl');
    echo '<br />Invalid login. Check username and/or password';
  }
  break;

  // -- JOBS PAGE --
  case 'jobs':
  //If template is not cached then grab industry jobs
  if(!$smarty->is_cached('jobs.tpl')) {
    $jobs = Functions::getIndustryJobs();
    $smarty->assign('jobs',$jobs);
  }
  $smarty->display('header.tpl');
  $smarty->display('jobs.tpl');
  break;

  // -- ASSETS PAGE --
  case 'assets':
  // Never cache assets page as it's too dynamic
  $smarty->clear_cache('assets.tpl');
  $select = '*';
  if(isset($_GET['w'])) {
    $where = 'WHERE containerID ='.$_GET['w'].'
    AND ownerID = 0
    GROUP BY flag,typeName';
    $select = 'itemID,containerID,locationID,typeID,typeName,sum(quantity) as quantity,flag,singleton';
    unset($_GET['w']);
  } else {
    $where = 'WHERE containerID IS NULL
    AND ownerID = 0';
  }
  $assets   = Functions::getConditionalAssets($select, $where, $order);
  $smarty->assign('assets',$assets);
  $smarty->display('header.tpl');
  $smarty->display('assets.tpl');
  break;

  // -- SHOPPING PAGE --
  case 'shopping':
  $select = "typeID, typeName, SUM(quantity) as quantity, flag";
  $where = "WHERE `typeName` LIKE '%II Blueprint%'";
  $order = "GROUP BY typeName";
  $t2bpcs = Functions::getConditionalAssets($select, $where, $order);
  $smarty->assign('bpcs',$t2bpcs);
  $smarty->display('header.tpl');
  $smarty->display('shopping.tpl');
  break;

  // -- INVENTION PAGE --
  case 'invention':
  $select = "`typeName`,COUNT(*) AS quantity,`flag`,`locationID`, `typeID`";
  $where = "WHERE `typeName` LIKE '% I Blueprint%' AND `containerID` IN (SELECT `itemID` FROM `assets`
  WHERE `typeName` IN ('Mobile Laboratory','Advanced Mobile Laboratory') AND `containerID` IS NULL)
  AND `flag` = (SELECT `hangerID` FROM `hangers` WHERE `hangerName` = 'Manufacturing')";
  $order = "GROUP BY `typeName`";
  $t1bpcs = Functions::getConditionalAssets($select,$where, $order);
  $smarty->assign('t1bpcs',$t1bpcs);
  $smarty->display('header.tpl');
  $smarty->display('invention.tpl');
  break;

  // -- MATERIALS PAGE -- (No direct link)
  case 'invent':
  // Never cache materials as it's too dynamic
  $smarty->clear_cache();
  if ($_GET['t'] == 'all') {
    $_GET['t'] = 0;
  }
  $materials = Functions::getRequiredMaterials($_GET['t'], $_GET['act'], $_GET['q']);
  $smarty->assign('materials',$materials);
  $smarty->display('header.tpl');
  $smarty->display('materials.tpl');
  break;

  default:
  $smarty->display('header.tpl');
  echo 'Not implemented';
  break;
}

$smarty->display('footer.tpl');
?>